<template>
    <div  :class="className" :style="{height:height,width:width}" />
  </template>
  
  <script>
  import * as echarts from 'echarts';
  require('echarts/theme/macarons') // echarts theme
  import resize from '../mixins/resize'
  
  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '400px'
      },
      autoResize: {
        type: Boolean,
        default: true
      },
      dataList:{
      type: Array,
      default: () => []
    },
    dateList:{
      type:Array,
      default: () => []
    },
    name:{
      type: String,
      default:'AQI'
    },
    },
    data() {
      return {
      }
    },
    watch: {
      chartData: {
        deep: true,
        handler(val) {
          console.log('触发了Line组件调用x轴初始化')

        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    watch: {
    dataList: {
      handler: function (val) {
        this.updateChart()
      },
      deep: true
    }
  },
    methods: {
      updateChart() {
      if (!this.chart) {
        return
      }
      this.chart.setOption({
        title:{
          text: this.name
        },
        xAxis:[{
          data:this.dateList
        }],
        series: [{
          name:this.name,
          data: this.dataList
        },{
          name:this.name,
          data: this.dataList
        }]
      })
      this.$emit('preLoad', false) 
      
    },
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
          
        this.setOptions(this.dataList,this.dateList)
        
  
      },
      setOptions(dataList,dateList) {
        this.chart.setOption({
          title:{
            text: this.name
          },
          xAxis: {
            data:dateList,
            type: 'category',
     
            splitLine:{
            show:true,
            // lineStyle:{
            //   width: 1.5,
            //   opacity: 0.3,
            //   type:'dashed',
            //   color:'black'
            // },
          },
            splitArea: {
            show: false
            },
            axisLine: {
            show: false,//不显示坐标轴线
      },
            boundaryGap: true,
            axisTick: {
              show: false
            },
            axisLabel:{
    
              textStyle:{
                fontSize:12,
                  color:"black"
                }
            },
  
  
          },
          grid: {
            top: 10,
                left: '2%',
      right: '3%',
      bottom: '3%',
      containLabel: true,
            // left: 10,
            // right: 10,
            // bottom: 20,
            top: 30
            // containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            // padding: [5, 10]
          },
          yAxis: [{
            type: 'value',
            axisTick: {
            show: false
          },
            splitLine:{
            show:true,
            lineStyle:{
              width: 2,
              opacity: 0.3,
              type:'dashed',
              color:'black'
            },
          },
            // 会盖住y轴
            splitArea: {
            show: false
            },
            axisLabel:{//修改坐标系字体颜色
                show:true,
                textStyle:{
                  fontSize:15,
                  color:"black"
                }
              },
  
          },
      
        ],
        //   legend: {
        //     data: ['PM2.5','CO','PM10','SO2','NO2','O3'],
        //     textStyle:{
        //           color:'black',
        //   },
        //   },
        toolbox: {
    feature: {
      // restore: {},
      saveAsImage: {},
    }
  },
          series: [
          {
            showSymbol: true,
            name: this.name,
            smooth: true,
            type: 'line',
            symbolSize:4,
            sampling: 'lttb',
            color:'#03A9F4',
            data: dataList,  
            animationDuration: 1500,
            animationEasing: 'quadraticOut',
                
          },
          {
            name: this.name,
            // showSymbol: true,
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            color:'#AFB42B',
            itemStyle:{
              barBorderRadius: [15, 15, 0, 0],
            },
            data: dataList,  
            animationDuration: 2000,
            animationEasing: 'quadraticOut',
            tooltip:{
              show:false
            }
          },
      ]
        })
      }
    }
  }
  </script>
  